import xgboost as xgb     
from xgboost import Booster as bst
from turtle import forward
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import cross_val_score
from sklearn.decomposition import IncrementalPCA
import m2cgen as m2c
import glob
from sklearn.metrics import accuracy_score
import os
import time


if __name__ == "__main__":
   while(1):  
      # 键入测试染料
      dyename = input("input name of dye:")
      dict1 = {"茶树果壳色素":[[70,20,10,0,0,0]],"冬青叶":[[80,0,20,0,0,0]],"荷叶":[[70,10,10,10,0,0]],"竹叶":[[60,0,0,0,40,0]],"红苋菜叶":[[50,0,50,0,0,0]],"乌饭树叶":[[0,0,10,0,0,90]],"红茶色素":[[0,0,0,0,0,100]],"茶叶/天然茶多酚":[[0,0,0,20,0,80]],"大血藤":[[0,0,0,30,0,70]],"黑枸杞色素":[[0,0,90,10,0,0]],"花青素粉末":[[0,0,100,0,0,0]],"紫甘薯":[[0,0,80,20,0,0]],"菱角壳":[[0,0,100,0,0,0]],"玫瑰茄花瓣色素":[[0,0,100,0,0,0]],"商陆科浆果":[[0,0,80,20,0,0]],"紫矿花/紫铆花/紫铆单精子":[[0,0,70,20,0,10]]
               ,"葡萄籽":[[0,0,20,10,0,70]],"乌饭树叶":[[0,0,10,0,0,90]],"银杏树叶/银杏叶/银杏落叶天然黄色素":[[0,10,10,80,0,0]],"栾树叶":[[10,70,0,20,0,0]],"南瓜皮色素":[[0,80,20,0,0,0]],"石榴皮/石榴色素/番石榴/番石榴色素":[[0,0,0,0,100,0]],"薯茛/柞树":[[0,0,0,0,100,0]],"红树树皮色素":[[0,0,0,0,80,20]],"核桃树皮/胡桃树皮/核桃木/胡桃木":[[0,0,0,0,90,10]],"胭脂虫/栎五倍子/栎瘿":[[0,0,0,0,100,0]],"红曲米红曲色素/决明子荚果/决明子果实":[[0,0,0,100,0,0]],"姜黄素":[[0,0,0,0,0,100]]}
      keys = [x[1] for x in dict1.items() if dyename in x[0]]
      names = [x[0] for x in dict1.items() if dyename in x[0]]
      if (keys != []):
        print("组分查找结果：",names,keys)
        print(names[0],"作为染料的组分在数据库中查找的结果为:","叶绿素:",keys[0][0][0],"%","叶黄素等类胡萝卜素 :",keys[0][0][1],"%","花青素花色苷:",keys[0][0][2],"%","其他黄酮类:",keys[0][0][3],"%","单宁等类黄酮:",keys[0][0][4],"%","原花青素等其他多酚类:",keys[0][0][5],"%")
        print("")
        ingredients = keys[0]
      else:
      #  print("未找到相应数据，请手动输入：")
        ingredients =[list(int(char) for char in input("未找到相应数据，请手动输入：").split('，'))]
        print("您自定义的染料组分为:","叶绿素:",ingredients[0][0],"%","叶黄素等类胡萝卜素 :",ingredients[0][1],"%","花青素花色苷:",ingredients[0][2],"%","其他黄酮类:",ingredients[0][3],"%","单宁等类黄酮:",ingredients[0][4],"%","原花青素等其他多酚类:",ingredients[0][5],"%")
        print(ingredients)
      # 定义测试数据
      # testing_inputs = x1,x2    
      # testing_inputs = [[0,0,7,2,0,1]]
      # testing_inputs = [[0,0,1,0,0,9]]
      testing_inputs = ingredients
      # testing_outputs = 3

      # 加载模型
      st = time.time()
      print("Loading XGBoost Model...")
      xgb_model = xgb.Booster(model_file='xgb_model_PH.model')
      xgb_model1 = xgb.Booster(model_file='xgb_model_CFW.model')
      xgb_model2 = xgb.Booster(model_file='xgb_model_CFR.model')
      # 进行预测
      print("Predicting...")
      print("Predicting...")
      print("Predicting...")
      # print("")
      predictions = xgb_model.predict(xgb.DMatrix(np.array(testing_inputs)))
      predictions1 = xgb_model1.predict(xgb.DMatrix(np.array(testing_inputs)))
      predictions2 = xgb_model2.predict(xgb.DMatrix(np.array(testing_inputs)))
      # threshold = 0.5  # 设置阈值，根据具体情况调整
      # predicted_labels = [1 if pred >= threshold else 0 for pred in predictions]
      print("Result Matrix:",predictions,predictions1,predictions2)
      print("""
            ===========Result==============
            """)
      ##################################
      if(predictions == 3):
       result = "反应最佳酸碱度（PH）预测:2~3"
      elif(predictions == 4):
       result = "反应最佳酸碱度（PH）预测:3~5"
      elif(predictions == 5):
       result = "反应最佳酸碱度（PH）预测:5~6"
      elif(predictions == 6):
       result = "反应最佳酸碱度（PH）预测:6+"
      else:
       result = "反应最佳酸碱度（PH）模型超出拟合范围"
      ##################################
      if(predictions1 == 3):
       result1 = "水洗色牢度预测:3~4"
      elif(predictions1 == 4):
       result1 = "水洗色牢度预测:4~5"
      elif(predictions1 == 5):
       result1 = "水洗色牢度预测:5"
      else:
       result1 = "水洗色牢度模型超出拟合范围"
      ##################################
      if(predictions2 == 3):
       result2 = "摩擦色牢度预测:3~4"
      elif(predictions2 == 4):
       result2 = "摩擦色牢度预测:4~5"
      elif(predictions2 == 5):
       result2 = "摩擦色牢度预测:5"
      else:
       result2 = "摩擦色牢度模型超出拟合范围"
      ##################################
      print(result,result1,result2)
      print("")
      et = time.time()
      print("推理运行时间：",et-st)







    

